﻿using Forum.Model.Entity.System;
using Forum.Model.Pager;
using Forum.Model.QueryFilters;
using Forum.Model.Request;
using Forum.Repository.Extensions;
using Forum.Repository.Interface;
using Forum.Service.Interface.System;

namespace Forum.Service.Service.System
{
    public class SysRoleService : ISysRoleService
    {
        private readonly IAppRepository<SYS_ROLE> _roleRepo;
        private readonly IAppRepository<SYS_USER_ROLE> _userRoleRepo;
        public SysRoleService(IAppRepository<SYS_ROLE> roleRepo, IAppRepository<SYS_USER_ROLE> userRoleRepo)
        {
            _roleRepo = roleRepo;
            _userRoleRepo = userRoleRepo;
        }

        /// <summary>
        /// 分页查询角色
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IPagedList<SYS_ROLE> GetPagedList(QueryParameters param)
        {
            var querySet = _roleRepo.SugarQueryable;

            // 关键词过滤
            if (!string.IsNullOrEmpty(param.Search))
            {
                querySet = querySet.Where(a => a.CODE.Contains(param.Search) || a.NAME.Contains(param.Search));
            }
            // 排序
            querySet = querySet.OrderByDescending(a => a.NAME);
            if (!string.IsNullOrEmpty(param.SortBy))
            {
                // 是否升序
                var ascending = !param.SortBy.StartsWith("-") ? " asc" : " desc";
                var orderByProperty = param.SortBy.Trim('-');

                querySet = querySet.OrderBy(orderByProperty + ascending);
            }
            var roleList = querySet.ToPage(param.Page, param.PageSize);

            return roleList;
        }

        /// <summary>
        /// 查询用户的角色
        /// </summary>
        /// <returns></returns>
        public List<SYS_ROLE> GetUserRoles(string? userId)
        {
            var roleList = this._roleRepo.Db.Queryable<SYS_ROLE>()
                 .InnerJoin<SYS_USER_ROLE>((a, b) => a.ID == b.ROLE_ID)
                 .InnerJoin<SYS_USER>((a, b, c) => b.USER_ID == c.UserId)
                 .WhereIF(!string.IsNullOrWhiteSpace(userId), (a, b, c) => (c.UserId == userId || c.UserName == userId || c.UserCode == userId))
                 .Where((a, b, c) => a.ISACTIVE == "Y" && b.ISACTIVE == "Y" && c.ISACTIVE == "Y")
                 .ToList();


            return roleList ?? new List<SYS_ROLE>();
        }

        /// <summary>
        /// 新增用户角色
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool AddUserRole(UserRoleReq req)
        {
            List<SYS_USER_ROLE> userRoleList = new List<SYS_USER_ROLE>();
            foreach (var roleId in req.RoleList)
            {
                SYS_USER_ROLE userRole = new SYS_USER_ROLE();
                userRole.ROLE_ID = roleId;
                userRole.USER_ID = req.UserId;
                userRole.REMARKS = req.Remark;
                userRole.ISACTIVE = "Y";

                userRoleList.Add(userRole);
            }

            return  _userRoleRepo.Insert(userRoleList);
        }


    }
}
